Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports Microsoft.Practices.EnterpriseLibrary.Data.Sql
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.Common

Namespace Seuss.EnterpriseLibrary.DataAccess

    Public Class Transaccion

#Region " IDisposable Support "

        Implements IDisposable
        Private disposedValue As Boolean = False

        Protected Overridable Sub Dispose(ByVal disposing As Boolean)
            If Not Me.disposedValue Then
                If disposing Then
                    Db = Nothing
                End If
            End If
            Me.disposedValue = True
        End Sub
        Public Sub Dispose() Implements IDisposable.Dispose
            Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub

#End Region

#Region "Propiedades"

        Private Db As Database
        ReadOnly Property DataBase() As Database
            Get
                Return Db
            End Get
        End Property
#End Region

#Region "Contructores"

        Public Sub New()
            Db = DatabaseFactory.CreateDatabase
        End Sub

        Public Sub New(ByVal NewDb As Database)
            Db = NewDb
        End Sub

#End Region

        'Registra las transacciones
        Public Shared Function Insert(ByVal Operacion As Long, _
                                      ByVal cPerCodigo As String, _
                                      ByVal RemoteHost As String, _
                                      ByVal Descripcion As String, _
                                      ByVal Valor As String) As Long
            Dim db As Database = DatabaseFactory.CreateDatabase()
            Try
                Using Cmd As SqlCommand = db.GetStoredProcCommand("sp_Transaction_Set")
                    Dim cTraCodigo As String = String.Empty
                    cTraCodigo = Format(Date.Now, "WEByyyyMMddhhmmssFF")
                    db.AddInParameter(Cmd, "@cTraCodigo", System.Data.DbType.String, cTraCodigo)
                    db.AddInParameter(Cmd, "@nOpeCodigo", System.Data.DbType.Int32, Operacion)
                    db.AddInParameter(Cmd, "@cPerCodigo", System.Data.DbType.StringFixedLength, cPerCodigo)
                    db.AddInParameter(Cmd, "@cTraHost", System.Data.DbType.String, RemoteHost)
                    db.AddInParameter(Cmd, "@cTraDescripcion", System.Data.DbType.String, Descripcion)
                    db.AddInParameter(Cmd, "@cTraValor", System.Data.DbType.String, Valor)
                    Return db.ExecuteNonQuery(Cmd)
                End Using

            Catch ex As SqlException
                Throw ex
            End Try
        End Function

        Public Function Transaccion_Ins(ByVal Operacion As Long, _
                                     ByVal cPerCodigo As String, _
                                     ByVal RemoteHost As String, _
                                     ByVal Descripcion As String, _
                                     Optional ByVal Valor As String = "", _
                                     Optional ByVal cPerCodigoDst As String = "", _
                                     Optional ByVal cDocCodigoDst As String = "") As SqlCommand
            Try
                Using Cmd As SqlCommand = Db.GetStoredProcCommand("sp_Transaction_Set")
                    Dim cTraCodigo As String = String.Empty
                    cTraCodigo = Format(Date.Now, "WEByyyyMMddhhmmssFF")
                    Db.AddInParameter(Cmd, "@cTraCodigo", System.Data.DbType.String, cTraCodigo)
                    Db.AddInParameter(Cmd, "@nOpeCodigo", System.Data.DbType.Int32, Operacion)
                    Db.AddInParameter(Cmd, "@cPerCodigo", System.Data.DbType.StringFixedLength, cPerCodigo)
                    Db.AddInParameter(Cmd, "@cTraHost", System.Data.DbType.String, RemoteHost)
                    Db.AddInParameter(Cmd, "@cTraDescripcion", System.Data.DbType.String, Descripcion)
                    Db.AddInParameter(Cmd, "@cTraValor", System.Data.DbType.String, Valor)
                    Db.AddInParameter(Cmd, "@cPerCodigoDst", System.Data.DbType.String, cPerCodigoDst)
                    Db.AddInParameter(Cmd, "@cDocCodigoDst", System.Data.DbType.String, cDocCodigoDst)
                    Return Cmd
                End Using

            Catch ex As SqlException
                Throw ex
            End Try
        End Function

        Public Function TransaccionOrigen_Ins(ByVal Operacion As Long, _
                                     ByVal cPerCodigo As String, _
                                     ByVal RemoteHost As String, _
                                     ByVal IP As String, _
                                     ByVal UrlOrigen As String, _
                                     ByVal UrlDestino As String, _
                                     ByVal UrlDestinoCompleto As String, _
                                     ByVal SO As String, _
                                     ByVal Navegador As String) As SqlCommand
            Try
                Using Cmd As SqlCommand = Db.GetStoredProcCommand("sp_TransactionOrigen_Set")
                    Dim cTraCodigo As String = String.Empty
                    cTraCodigo = Format(Date.Now, "WEByyyyMMddhhmmssFF")
                    Db.AddInParameter(Cmd, "@cTraCodigo", System.Data.DbType.String, cTraCodigo)
                    Db.AddInParameter(Cmd, "@nOpeCodigo", System.Data.DbType.Int32, Operacion)
                    Db.AddInParameter(Cmd, "@cPerCodigo", System.Data.DbType.StringFixedLength, cPerCodigo)
                    Db.AddInParameter(Cmd, "@cTraHost", System.Data.DbType.String, RemoteHost)
                    Db.AddInParameter(Cmd, "@cTraIP", System.Data.DbType.String, IP)
                    Db.AddInParameter(Cmd, "@cTraUrlOrigen ", System.Data.DbType.String, UrlOrigen)
                    Db.AddInParameter(Cmd, "@cTraUrlDestino", System.Data.DbType.String, UrlDestino)
                    Db.AddInParameter(Cmd, "@cTraUrlDestinoCompleto", System.Data.DbType.String, UrlDestinoCompleto)
                    Db.AddInParameter(Cmd, "@cSO", System.Data.DbType.String, SO)
                    Db.AddInParameter(Cmd, "@cNavegador", System.Data.DbType.String, Navegador)
                    Return Cmd
                End Using

            Catch ex As SqlException
                Throw ex
            End Try
        End Function

        Public Function TransaccionRating_Ins(ByVal Operacion As Long, _
                                     ByVal cPerCodigo As String, _
                                     ByVal RemoteHost As String, _
                                     ByVal Descripcion As String, _
                                     Optional ByVal Valor As String = "", _
                                     Optional ByVal cPerCodigoDst As String = "", _
                                     Optional ByVal cDocCodigoDst As String = "") As SqlCommand
            Try
                Using Cmd As SqlCommand = Db.GetStoredProcCommand("sp_TransactionRating_Set")
                    Dim cTraCodigo As String = String.Empty
                    cTraCodigo = Format(Date.Now, "WEByyyyMMddhhmmssFF")
                    Db.AddInParameter(Cmd, "@cTraCodigo", System.Data.DbType.String, cTraCodigo)
                    Db.AddInParameter(Cmd, "@nOpeCodigo", System.Data.DbType.Int32, Operacion)
                    Db.AddInParameter(Cmd, "@cPerCodigo", System.Data.DbType.StringFixedLength, cPerCodigo)
                    Db.AddInParameter(Cmd, "@cTraHost", System.Data.DbType.String, RemoteHost)
                    Db.AddInParameter(Cmd, "@cTraDescripcion", System.Data.DbType.String, Descripcion)
                    Db.AddInParameter(Cmd, "@cTraValor", System.Data.DbType.String, Valor)
                    Db.AddInParameter(Cmd, "@cPerCodigoDst", System.Data.DbType.String, cPerCodigoDst)
                    Db.AddInParameter(Cmd, "@cDocCodigoDst", System.Data.DbType.String, cDocCodigoDst)
                    Return Cmd
                End Using

            Catch ex As SqlException
                Throw ex
            End Try
        End Function


        Public Function TransaccionReproducir_Ins(ByVal Operacion As Long, _
                                     ByVal cPerCodigo As String, _
                                     ByVal RemoteHost As String, _
                                     ByVal Descripcion As String, _
                                     Optional ByVal Valor As String = "", _
                                     Optional ByVal TransaccionActual As String = "", _
                                     Optional ByVal cPerCodigoDst As String = "", _
                                     Optional ByVal cDocCodigoDst As String = "") As SqlCommand
            Try
                Using Cmd As SqlCommand = Db.GetStoredProcCommand("sp_TransactionReproducir_Set")
                    Dim cTraCodigo As String = String.Empty
                    cTraCodigo = Format(Date.Now, "WEByyyyMMddhhmmssFF")
                    Db.AddInParameter(Cmd, "@cTraCodigo", System.Data.DbType.String, cTraCodigo)
                    Db.AddInParameter(Cmd, "@nOpeCodigo", System.Data.DbType.Int32, Operacion)
                    Db.AddInParameter(Cmd, "@cPerCodigo", System.Data.DbType.StringFixedLength, cPerCodigo)
                    Db.AddInParameter(Cmd, "@cTraHost", System.Data.DbType.String, RemoteHost)
                    Db.AddInParameter(Cmd, "@cTraDescripcion", System.Data.DbType.String, Descripcion)
                    Db.AddInParameter(Cmd, "@cTraValor", System.Data.DbType.String, Valor)
                    Db.AddInParameter(Cmd, "@cTransaccionActual", System.Data.DbType.String, TransaccionActual)
                    Db.AddInParameter(Cmd, "@cPerCodigoDst", System.Data.DbType.String, cPerCodigoDst)
                    Db.AddInParameter(Cmd, "@cDocCodigoDst", System.Data.DbType.String, cDocCodigoDst)
                    Return Cmd
                End Using

            Catch ex As SqlException
                Throw ex
            End Try
        End Function

        Public Function Transaccion_Ins_Sesion(ByVal Operacion As Long, _
                                    ByVal cPerCodigo As String, _
                                    ByVal RemoteHost As String, _
                                    ByVal Descripcion As String, _
                                    Optional ByVal Valor As String = "", _
                                     Optional ByVal cPerCodigoDst As String = "", _
                                     Optional ByVal cDocCodigoDst As String = "") As SqlCommand
            Try
                Using Cmd As SqlCommand = Db.GetStoredProcCommand("sp_TransactionSesion_Set")
                    Dim cTraCodigo As String = String.Empty
                    cTraCodigo = Format(Date.Now, "WEByyyyMMddhhmmssFF")
                    Db.AddInParameter(Cmd, "@cTraCodigo", System.Data.DbType.String, cTraCodigo)
                    Db.AddInParameter(Cmd, "@nOpeCodigo", System.Data.DbType.Int32, Operacion)
                    Db.AddInParameter(Cmd, "@cPerCodigo", System.Data.DbType.StringFixedLength, cPerCodigo)
                    Db.AddInParameter(Cmd, "@cTraHost", System.Data.DbType.String, RemoteHost)
                    Db.AddInParameter(Cmd, "@cTraDescripcion", System.Data.DbType.String, Descripcion)
                    Db.AddInParameter(Cmd, "@cTraValor", System.Data.DbType.String, Valor)
                    Db.AddInParameter(Cmd, "@cPerCodigoDst", System.Data.DbType.String, cPerCodigoDst)
                    Db.AddInParameter(Cmd, "@cDocCodigoDst", System.Data.DbType.String, cDocCodigoDst)
                    Return Cmd
                End Using

            Catch ex As SqlException
                Throw ex
            End Try
        End Function
    End Class

End Namespace
